package com.service.impl;

import com.Dao.PersonDao;
import com.Result.ResultData;
import com.entity.Person;
import com.service.PersonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("personService")
public class PersonServiceImpl implements PersonService {
    @Resource
    private PersonDao personDao;
    @Override
    public int addPerson(Person person) {
        return personDao.addPerson(person);
    }
    public int updatePerson(Person person){
        return  personDao.updatePerson(person);
    }
    @Override
    public Person getPersonById(int user_id) {
        return personDao.getPersonById(user_id);
    }

    @Override
    public Person getPersonByName(String user_name) {
        return personDao.getPersonByName(user_name);
    }

    @Override
    public int countPersonNum() {
        return personDao.countPersonNum();
    }
    @Override
    public int deletePerson(Person person) {
        return personDao.deletePerson(person);
    }

    @Override
    public ResultData Register(String user_name, String user_password, int user_sex, int user_age,String user_pro, String user_hobby) {
        ResultData resultData=new ResultData();
        if(getPersonByName(user_name)==null)
        {
            Person person=new Person();
            int tempId=countPersonNum()+1;
            person.setUser_id(tempId);
            person.setUser_name(user_name);
            person.setUser_password(user_password);
            person.setUser_sex(user_sex);
            person.setUser_age(user_age);
            person.setUser_pro(user_pro);
            person.setUser_hobby(user_hobby);
            person.setFollowing_list("");
            person.setFan_list("");
            person.setFollowing_num(0);
            person.setFan_num(0);
            person.setUser_lock(0);
            addPerson(person);
            resultData.setStatus(200);
            resultData.setMessage("注册成功！");
            resultData.setData(person);
        }else{
            resultData.setStatus(400);
            resultData.setMessage("注册失败，用户名重复！");
        }
        return resultData;
    }

    @Override
    public ResultData Login(String user_name, String user_password) {
        ResultData resultData=new ResultData();
        Person person=getPersonByName(user_name);
        if(person==null||!person.getUser_password().equals(user_password))
        {
            resultData.setStatus(400);
            resultData.setMessage("登录失败，用户不存在或密码错误！");
        }else{
            resultData.setStatus(200);
            resultData.setMessage("登录成功！");
        }
        return resultData;
    }
}
